<template>
  <div class="content">
    <headline v-bind:title="title"></headline>
    <div class="filter">
      <el-form :model="formInline">
        <el-row>
          <el-col :span="7">
            <el-select size="medium" v-model="formInline.value" placeholder="请选择" class="input">
              <el-option label="按商品id查询" value="id"></el-option>
              <el-option label="按商品名称查询" value="name"></el-option>
            </el-select>
          </el-col>
          <el-col :span="7">
            <el-input size="medium" v-model="formInline.user" placeholder="请输入" class="input"></el-input>
          </el-col>
          <el-col :span="7">
            <el-button type="primary" size="medium" icon="el-icon-search" @click="search()">查询</el-button>
          </el-col>
          <el-col :span="3">
            <el-button type="primary" size="medium" icon="el-icon-plus" @click="append()">添加商品</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="data-table">
      <el-table :data="queryList.data" border style="width: 100%" max-height="500">
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="name" label="信息"></el-table-column>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <span style="padding-right: 10px">{{ scope.row.status === 1 ? "在售" : "已下架" }}</span>
            <el-button size="mini" type="warning" @click="market(scope.row.status, scope.row.id)">
              {{ scope.row.status === 1 ? "下架" : "上架" }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="check(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="redact(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <paging v-bind:queryList="this.queryList"></paging>
  </div>
</template>
<script>
import headline from "../headline.vue";
import paging from "../paging.vue";

export default {
  components: {headline, paging},
  data() {
    return {
      title: "商品管理",
      queryList: {data: [], total: 0, pageNum: 1, pageSize: 10},
      formInline: {user: "", value: "id"},
    };
  },
  mounted() {
    this.inquire();
  },
  methods: {
    handleSizeChange(val) {
      this.queryList.pageSize = val;
      this.inquire();
    },
    handleCurrentChange(val) {
      this.queryList.pageNum = val;
      this.inquire();
    },
    // 查询方法
    inquire() {
      let postData = this.qs.stringify({pageSize: this.queryList.pageSize, pageNum: this.queryList.pageNum});
      this.axios({method: "POST", url: "/api/manage/product/list.do", data: postData}).then((res) => {
        this.queryList.data = res.data.data.list;
        this.queryList.total = res.data.data.total;
      });
    },
    // 按条件搜索数据
    search() {
      if (this.formInline.value === "id") {
        let postData = this.qs.stringify({productId: this.formInline.user});
        this.axios({method: "POST", url: "/api/manage/product/search.do", data: postData}).then((res) => {
          this.queryList.data = res.data.data.list;
          this.queryList.total = res.data.data.total;
        });
      } else {
        let postData = this.qs.stringify({productName: this.formInline.user});
        this.axios({method: "POST", url: "/api/manage/product/search.do", data: postData}).then((res) => {
          this.queryList.data = res.data.data.list;
          this.queryList.total = res.data.data.total;
        });
      }
    },
    // 上下架商品
    market(stat, id) {
      if (stat === 1) {
        this.$confirm("确认要下架改商品", "提示", {
          confirmButtonText: "确定", cancelButtonText: "取消", type: "warning",
        }).then(() => {
          let data = this.qs.stringify({productId: id, status: 2,})
          this.axios.post("/api/manage/product/set_sale_status.do", data).then(() => {
            this.search();
            this.$message({type: "success", message: "修改产品状态成功",});
          });
        }).catch(() => {
          this.$message({type: "info", message: "已取消下架",})
        });
      } else {
        this.$confirm("确认要上架改商品", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          let data = this.qs.stringify({productId: id, status: 1,})
          this.axios.post("/api/manage/product/set_sale_status.do", data).then(() => {
            this.search();
            this.$message({type: "success", message: "修改产品状态成功"});
          });
        }).catch(() => {
          this.$message({type: "info", message: "已取消上架",});
        });
      }
    },
    //添加商品
    append() {
      this.$router.push({name: "addgoogds", params: {id: null, public: "add"},});
    },
    //查看商品
    check(row) {
      this.$router.push({name: "addgoogds", params: {id: row.id, public: "view"}});
    },
    //编辑商品
    redact(row) {
      this.$router.push({name: "addgoogds", params: {id: row.id, public: "edit"}});
    },
  },
};
</script>